<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use think\Hook;

/*主体内容部分*/
class Main extends AdminBase
{
	public function _initialize(){
    	$filter=array("login","login_post","login_out","index");
    	if(!in_array(request()->action(),$filter)){
    		parent::_initialize();
    	}
    }
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	
	public function index(){
		return $this->fetch();
	}
	/*登录页面*/
	public function login(){
		$this->assign("soft_version",input("soft_version","未知"));
		if(!empty(input("shop_id"))){
			$this->assign("shop_id",input("shop_id"));
			$this->assign("login_type",input("login_type"));//登录类型
		}else{
			$this->assign("shop_id",0);//店铺id
			$this->assign("login_type",0);//登录类型  0  pc  1 客户端
		}
		if(!empty(stristr($_SERVER['SERVER_NAME'],'smartgymbackup'))){
			return $this->fetch("./public/admin/login.html");
		}
		if(empty(input("login_type"))&&(request()->ip()!="127.0.0.1")&&strpos(request()->ip(), "192.168")===FALSE){
			if(!(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')){
				if(empty(stristr($_SERVER['SERVER_NAME'], "smartgymdemo"))){
					return $this->redirect("https://smartgym.ocmcom.com/");	
				}else{
					return $this->redirect("https://smartgymdemo.ocmcom.com/");
				}
			}
		}
		return $this->fetch("./public/admin/login.html");
	}
	/*登录post*/
	public function login_post(){
		$this->_login_post();
	}
	/*退出*/
	public function login_out(){
		session(null);
		if(isset($_COOKIE['s_d'])){
			$this->redirect('/Admin/Main/login?login_type='.$_COOKIE['l_t']."&shop_id=".$_COOKIE['s_d']."&soft_version=".$_COOKIE['s_v']);
		}else{
			$this->redirect('/Admin/Main/login');	
		}
	}
	public function clear_cache(){
		$shop_id=sp_get_current_shop_id();
		cache("ExtraConfig_".$shop_id,null);
		cache("ShopConfig_".$shop_id,null);
		cache("SystemSetConfig_".$shop_id,null);
		cache("BikeOrder_".$shop_id,null);
		cache("BikeOrderDevice_".$shop_id,null);
		cache("SlidingDoor_".$shop_id,null);
		cache("ShopStatus_".$shop_id,null);
		
		cache("ReportForm",null);
		cache("TodayReportForm",null);
		cache("ShopsNameList",null);
		$this->ajax_success("清理成功");
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		if(empty(sp_get_current_admin())){
    		 $this->redirect("/Admin/Main/login?login_type=".$_COOKIE['l_t']."&shop_id=".$_COOKIE['s_d']."&soft_version=".$_COOKIE['s_v']);
    	}
		$auth_group=sp_get_auth_menu();
		$this->assign("menus",$auth_group);
		$this->assign("admin",sp_get_current_admin());
	}
	function _login_post(){
		$account=input('account/s');
		$psw=md5(input('psw/s'));
		$code=input('code/s');
		if(empty(input("login_type"))){
			if(!captcha_check($code)){
				$this->ajax_error("验证码错误");
			}
		}
		if(empty(input('account/s'))||empty(input('psw/s'))){
			$this->ajax_error('参数错误！');
		}else{
			
			$admin=db("admin")->where("account='$account' and password='$psw'")->fetchsql(FALSE)->find();
			if(empty($admin)){
				$this->ajax_error("账号或者密码错误！");
			}else{
				$shopStatus=db("Shop")->where("id=".$admin['shop_id'])->value("status");
				if(empty($shopStatus)){
					 return	$this->ajax_error("您的场馆账号异常！请和管理员联系！");
				}
				if(!empty(input("shop_id"))){
					if(input("shop_id")!=$admin['shop_id']){
					 return	$this->ajax_error("账号或者密码错误_！");
					}
				}
				session("Admin",$admin);
				db("AdminLog")->insert(array("admin_id"=>$admin['id'],"soft_version"=>input("soft_version"),"shop_id"=>$admin['shop_id'],"ip"=>request()->ip()));
				$this->ajax_success("验证通过！",array("s_d"=>$admin['shop_id'],"s_v"=>input("soft_version"),"l_t"=>input("login_type")));
			}
		}
	}
}
